package service_edu.demo;

import java.awt.Choice;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.POIXMLProperties.CoreProperties;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.Bookmark;
import org.apache.poi.hwpf.usermodel.Bookmarks;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Section;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;
import org.apache.poi.ss.usermodel.Table;
import org.apache.poi.xwpf.usermodel.BodyElementType;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.junit.Test;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
import org.springframework.beans.factory.annotation.Autowired;

import com.itextpdf.text.pdf.PdfStructTreeController.returnType;
import com.zeta.eduservice.entity.question.EduQuestion;
import com.zeta.eduservice.entity.question.EduQuestionChoice;
import com.zeta.eduservice.entity.vo.front.front.WordStringInfoVo;
import com.zeta.eduservice.utils.SUtils;

import javassist.bytecode.CodeIterator.Gap;

public class wordTest2 {
	@Autowired
	SUtils StringUtils;

	public void writeDocx(String path, Map<String, String> map) throws Exception {
		InputStream is = new FileInputStream(path);
		XWPFDocument doc = new XWPFDocument(is);
		// XWPFWordExtractor extractor = new XWPFWordExtractor(doc) ;
		// String text = extractor.getText();
		// System.out.println(text);
		// CoreProperties coreProps = extractor.getCoreProperties();
		// this.printCoreProperties(coreProps);
		// this.close(is);
	}

	/**
	 * 替换段落里面的变量
	 * 
	 * @param doc    要替换的文档
	 * @param params 参数
	 */
	private void replaceInPara(XWPFDocument doc, Map<String, Object> params) {
		Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();
		XWPFParagraph para;
		while (iterator.hasNext()) {
			para = iterator.next();
			this.replaceInPara(para, params);
		}
	}

	/**
	 * 替换段落里面的变量
	 * 
	 * @param para   要替换的段落
	 * @param params 参数
	 */
	private void replaceInPara(XWPFParagraph para, Map<String, Object> params) {
		List<XWPFRun> runs;
		Matcher matcher;
		if (this.matcher(para.getParagraphText()).find()) {
			runs = para.getRuns();
			for (int i = 0; i < runs.size(); i++) {
				XWPFRun run = runs.get(i);
				String runText = run.toString();
				matcher = this.matcher(runText);
				if (matcher.find()) {
					while ((matcher = this.matcher(runText)).find()) {
						runText = matcher.replaceFirst(String.valueOf(params.get(matcher.group(1))));
					}
					// 直接调用XWPFRun的setText()方法设置文本时，在底层会重新创建一个XWPFRun，把文本附加在当前文本后面，
					// 所以我们不能直接设值，需要先删除当前run,然后再自己手动插入一个新的run。
					para.removeRun(i);
					if (runText.equals("null")) {
						runText = "";
					}

					para.insertNewRun(i).setText(runText);
				}
			}
		}
	}

	/**
	 * 替换表格里面的变量
	 * 
	 * @param doc    要替换的文档
	 * @param params 参数
	 */
	private void replaceInTable(XWPFDocument doc, Map<String, Object> params) {
		Iterator<XWPFTable> iterator = doc.getTablesIterator();
		XWPFTable table;
		List<XWPFTableRow> rows;
		List<XWPFTableCell> cells;
		List<XWPFParagraph> paras;
		while (iterator.hasNext()) {
			table = iterator.next();
			rows = table.getRows();
			for (XWPFTableRow row : rows) {
				cells = row.getTableCells();
				for (XWPFTableCell cell : cells) {

					String cellTextString = cell.getText();
					for (Entry<String, Object> e : params.entrySet()) {
						if (cellTextString.contains("${" + e.getKey() + "}"))
							cellTextString = cellTextString.replace("${" + e.getKey() + "}", e.getValue().toString());
					}
					cell.removeParagraph(0);
					if (cellTextString.contains("${") && cellTextString.contains("}")) {
						cellTextString = "";
					}
					cell.setText(cellTextString);
//	                    paras = cell.getParagraphs();
//	                    for (XWPFParagraph para : paras) {
//	                        this.replaceInPara(para, params);
//	                    }

				}
			}
		}
	}

	/**
	 * 正则匹配字符串
	 * 
	 * @param str
	 * @return
	 */
	private Matcher matcher(String str) {
		Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(str);
		return matcher;
	}

	/**
	 * 关闭输入流
	 * 
	 * @param is
	 */
	private void close(InputStream is) {
		if (is != null) {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 关闭输出流
	 * 
	 * @param os
	 */
	private void close(OutputStream os) {
		if (os != null) {
			try {
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 输出CoreProperties信息
	 * 
	 * @param coreProps
	 */
	private void printCoreProperties(CoreProperties coreProps) {
		System.out.println(coreProps.getCategory()); // 分类
		System.out.println(coreProps.getCreator()); // 创建者
		System.out.println(coreProps.getCreated()); // 创建时间
		System.out.println(coreProps.getTitle()); // 标题
	}

	/**
	 * word占位用${object}有缺陷不能填充图片
	 * 
	 * @param filePath
	 * @param params
	 * @throws Exception
	 */
	public void templateWrite(String filePath, Map<String, String> params, String outFilePath) throws Exception {
		InputStream is = new FileInputStream(filePath);
		XWPFDocument doc = new XWPFDocument(is);
		XWPFTable table = doc.createTable(2, 7);
		table.createRow();
		List<XWPFTableRow> rows = table.getRows();
		// 表格属性
		CTTblPr tablePr = table.getCTTbl().addNewTblPr();
		// 表格宽度
		CTTblWidth width = tablePr.addNewTblW();
		width.setW(BigInteger.valueOf(8000));
		XWPFTableRow row;
		List<XWPFTableCell> cells;
		XWPFTableCell cell;
		int rowSize = rows.size();
		int cellSize;
		int n2 = 0;

		for (int i = 0; i < rowSize; i++) {
			row = rows.get(i);
			// 新增单元格
			row.addNewTableCell();
			// 设置行的高度
			row.setHeight(500);
			// 行属性
//	       CTTrPr rowPr = row.getCtRow().addNewTrPr();  
			// 这种方式是可以获取到新增的cell的。
//	       List<CTTc> list = row.getCtRow().getTcList();  
			cells = row.getTableCells();
			cellSize = cells.size();
			for (int j = 0; j < cellSize; j++) {
				cell = cells.get(j);
//	                cell.setColor("ff0000"); //红色  
				// 单元格属性
				if (i == 0) {
					if (j == 0)
						cell.setText("题 号");
					else if (j > 0 && j < cellSize - 2) {
						cell.setText("1");
					} else if (j == cellSize - 2) {
						cell.setText("总 分");
					} else if (j == cellSize - 1) {
						cell.setText("复核人");
					}
				} else if (i == 1) {
					if (j == 0) {
						cell.setText("得 分");
					}
				} else if (i == 2) {
					if (j == 0) {
						cell.setText("阅卷人");
					}
				}
				CTTc cttc = cell.getCTTc();
				CTTcPr ctPr = cttc.addNewTcPr();
				ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
				ctPr.addNewTcW().setW(BigInteger.valueOf(1100));
				cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
			}
		}
		// 替换段落里面的变量
//	        replaceInPara(doc, params);
//		Iterator<XWPFParagraph> itPara = doc.getParagraphsIterator();
//		while (itPara.hasNext()) {
//			XWPFParagraph paragraph = (XWPFParagraph) itPara.next();
//			List<XWPFRun> runs = paragraph.getRuns();
//			for (int i = 0; i < runs.size(); i++) {
//				String oneparaString = runs.get(i).getText(runs.get(i).getTextPosition());
//				for (Map.Entry<String, String> entry : params.entrySet()) {
//					oneparaString = oneparaString.replace(entry.getKey(), entry.getValue());
//				}
//				runs.get(i).setText(oneparaString, 0);
//			}
//		}

//	        // 替换表格里面的变量
//	        replaceInTable(doc, params);

		OutputStream os = new FileOutputStream(outFilePath);
		doc.write(os);
		os.flush();
		os.close();
	}

	/**
	 * word占位用{{object}}比较完美可以填充图片
	 * 
	 * @param filePath
	 * @param params
	 * @param outFilePath
	 * @return
	 * @throws Exception
	 */

	@Test
	public void test2() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("${NAME}", "王五王五啊柯乐义的辣味回答侯何问起网");
		map.put("${NsAME}", "王五王五啊王力味回答侯何问起网");
		map.put("${NAMaE}", "王五王五啊柯乐义侯何问起网");
		map.put("${NArME}", "王五王五啊柯乐义的辣味回答东拉网");
		map.put("${NwAME}", "王五王五啊王的辣味回答侯何问起网");
//	  params.put("localPicture", new PictureRenderData(120, 120, "D:\\A.png"));
		templateWrite(
				"C:\\Users\\Administrator\\Desktop\\TestDB-SpringBoot\\TestDB_parent\\file\\publicFile\\TestWord.docx",
				map, "C:\\Users\\Administrator\\Desktop\\TestDB-SpringBoot\\TestDB_parent\\file\\publicFile\\5.docx");

	}

	@Test
	public void test() throws Exception {
		InputStream is = new FileInputStream(docPath);
		HWPFDocument doc = new HWPFDocument(is);
		Range range = doc.getRange();
		AnalysisWord(range);
		

	}

	private void AnalysisWord(Range range) {
		// TODO Auto-generated method stub
		this.MaxI = range.numParagraphs();
		List<WordStringInfoVo> list = new ArrayList<WordStringInfoVo>();
		while (true) {
			boolean titleNumber = getTitleNumber(range, list);
			if (!titleNumber) {
				break;
			}
		}
	}

	int i = 0;
	int MaxI = 0;
	int CurrTitleNumber = 0;

	short TitleNumber1 = 0;

	String p2 = "\\(  [A-D]  \\)";
	String p3 = "[一二三四五六七八九十]、[\\u4E00-\\u9FA5\\s]{1,}";
	String p33 = "([一|二|三|四|五|六|七|八|九|十]{1,3})([、.]{1})([\\u4E00-\\u9FA5\\s]+题)";

	Pattern compile;
	Matcher matcher;
	String docDestPath = "C:\\Users\\Administrator\\Desktop\\路由与交换练习题.doc";
	String docPath = "C:\\Users\\Administrator\\Desktop\\a周规划\\TextDBSystemProject19.12.7\\路由与交换练习题.doc";

	private boolean isgetSimple(String s) {
		// TODO Auto-generated method stub
		compile = Pattern.compile("简要|说明|？|请(讲述|问)|给出|什么|简述");
		matcher = compile.matcher(s);
		if (matcher.find()) {
			
			return true;
		}
		return false;
	}

	private boolean isgetSimpleAnswer(String s) {
		// TODO Auto-generated method stub
		compile = Pattern.compile("答：");
		matcher = compile.matcher(s);
		if (matcher.find()) {
			return true;
		}
		return false;
	}

	public boolean getChoice(String s, EduQuestionChoice eduQuestionChoice) {

		compile = Pattern.compile("  [ABCD]  ");
		matcher = compile.matcher(s);
		if (matcher.find()) {
			String answer = matcher.group();
			answer = answer.trim();
			eduQuestionChoice.setContent(s).setAnswer(answer);
			return true;
		}
		return false;
	}

	public boolean getTitleGapNumber(Range range, String GapNumber1) {
		EduQuestion eduQuestion = new EduQuestion();
		
		String numberTitle = getNumberTitle(GapNumber1);
		if(numberTitle.equals("")) {
			
				eduQuestion.setContent(GapNumber1);
				eduQuestion.setType("填空");
				return true;
			
		}
		
		return false;
	}

	public boolean getTitleSimpleNumber(Range range, String SimpleNumber1) {
		EduQuestion eduQuestion = new EduQuestion();
			boolean simple = isgetSimple(SimpleNumber1);
			if (simple) {
				eduQuestion.setContent(SimpleNumber1);
				eduQuestion.setType("简答");
				String answerNumber2 = null;
				answerNumber2 = GetNextParagraph(range, answerNumber2);
				boolean simpleAnswer = isgetSimpleAnswer(answerNumber2);
				if (simpleAnswer) {
					String CurrAnswer = "";
					while (true) {
						CurrAnswer += answerNumber2;
						answerNumber2 = GetNextParagraph(range, answerNumber2);
						String isNumberTitle = getNumberTitle(answerNumber2);
						boolean simple2 = isgetSimple(answerNumber2);
						if (simple2||!isNumberTitle.equals("")) {
							eduQuestion.setAnswer(CurrAnswer);
					i--;
							break;
						}
						
					}
					
				}else {
					i--;
				}
				System.out.println(eduQuestion.toString());
				int temp = i;
				int kk = 0;
				while (kk < 6) {
					kk++;
					answerNumber2 = GetNextParagraph(range, answerNumber2);
					boolean is = isgetSimple(answerNumber2);
					if (!is) {
						if (kk == 6) {
							i = temp;
							return false;
						}
					} else {
						i = temp;
						return true;
					}

				}
				
			
			}
			
		
		return false;
	}

	public boolean getTitleFalseRightNumber(Range range, String GapNumber1) {
		EduQuestion eduQuestion = new EduQuestion();
		GapNumber1 = getNumberTitle(GapNumber1);
		if (GapNumber1.equals("")) {
			eduQuestion.setContent(GapNumber1);
			return true;
		}
		return false;
	}

	public boolean getTitleChoiceNumber(Range range, String ChoiceNumber1) {
		EduQuestionChoice eduQuestionChoice = new EduQuestionChoice();
		String ChoiceNumber2 = null;
		for (int j = 0; j < 4; j++) {
			ChoiceNumber2 = GetNextParagraph(range, ChoiceNumber2);
			if (j == 0) {
				boolean choice = getChoice(ChoiceNumber1, eduQuestionChoice);

				if (choice) {
					CurrTitleNumber++;
					eduQuestionChoice.setChoiceA(ChoiceNumber2);
				} else {
					return true;
				}
			} else if (j == 1) {
				eduQuestionChoice.setChoiceB(ChoiceNumber2);
			} else if (j == 2) {
				eduQuestionChoice.setChoiceC(ChoiceNumber2);
			} else if (j == 3) {
				eduQuestionChoice.setChoiceD(ChoiceNumber2);
			}

		}
System.out.println(eduQuestionChoice.toString());
		int temp = i;
		int kk = 0;
		while (kk < 6) {
			kk++;
			ChoiceNumber2 = GetNextParagraph(range, ChoiceNumber2);
			boolean choice = isgetChoice(ChoiceNumber2);
			if (!choice) {
				kk++;
				if (kk == 6) {
					i = temp;
					return false;
				}
			} else {
				i = temp;
				return true;
			}

		}
		return false;
	}

	public boolean getTitleNumber(Range range, List<WordStringInfoVo> list) {
		String CurrTitle = null;
		CurrTitle = GetNextParagraph(range, CurrTitle);

		String numberTitle = getNumberTitle(CurrTitle);
		if (!numberTitle.equals("")) {
			WordStringInfoVo wordStringInfoVo = new WordStringInfoVo();
			wordStringInfoVo.setNumberTitle(numberTitle);
			list.add(wordStringInfoVo);
			TitleNumber1++;
			CurrTitleNumber = 0;
			CurrTitle = numberTitle;
			if (!CurrTitle.equals(""))
				if (isChoiceTitle(numberTitle)) {
					String ChoiceNumber1 = null;
					while (true) {
						ChoiceNumber1 = GetNextParagraph(range, ChoiceNumber1);
						boolean titleChoiceNumber = getTitleChoiceNumber(range, ChoiceNumber1);

						if (!titleChoiceNumber) {
							break;
						}
					}
				} else if (isGapTitle(numberTitle)) {
					String GapNumber1 = null;
					while (true) {
						GapNumber1 = GetNextParagraph(range, GapNumber1);
						boolean titleChoiceNumber = getTitleGapNumber(range, GapNumber1);

						if (!titleChoiceNumber) {
							break;
						}
					}
				} else if (isFalseRightTitle(numberTitle)) {

				} else if (isSimpleTitle(numberTitle)) {
					String SimpleNumber1 = null;
					while (true) {
						SimpleNumber1 = GetNextParagraph(range, SimpleNumber1);
						boolean titleNumber = getTitleSimpleNumber(range, SimpleNumber1);

						if (!titleNumber) {
							break;
						}
					}
				}

		}
		int kk = 0;
		int temp = i;
		while (kk < 6) {
			kk++;
			CurrTitle = GetNextParagraph(range, CurrTitle);
			numberTitle = getNumberTitle(CurrTitle);
			if (numberTitle.equals("")) {
				if (kk == 6) {
					i = temp;
					return false;
				}
			} else {
				i = temp;
				return true;
			}

		}
		return false;

	}

	public String GetNextParagraph(Range range, String s) {
		while (true) {
			if (i < MaxI) {
				s = range.getParagraph(i++).text();
				if (isEmpty(s)) {
					continue;
				} else {
					break;
				}
			} else {
				break;
			}

		}
		return s;
	}

	public boolean isGapTitle(String s) {
		String p1 = "填空";
		compile = Pattern.compile(p1);
		matcher = compile.matcher(s);
		if (matcher.find()) {
			return true;
		}
		return false;

	}

	public boolean isFalseRightTitle(String s) {
		String p1 = "判断";
		compile = Pattern.compile(p1);
		matcher = compile.matcher(s);
		if (matcher.find()) {
			return true;
		}
		return false;

	}

	public boolean isSimpleTitle(String s) {
		String p1 = "简答";
		compile = Pattern.compile(p1);
		matcher = compile.matcher(s);
		if (matcher.find()) {
			return true;
		}
		return false;

	}

	public boolean isChoiceTitle(String s) {
		String p1 = "选择";
		compile = Pattern.compile(p1);
		matcher = compile.matcher(s);

		if (matcher.find()) {
			return true;
		}
		return false;

	}

	public boolean isgetChoice(String s) {

		compile = Pattern.compile("  [ABCD]  ");
		matcher = compile.matcher(s);
		if (matcher.find()) {
			return true;
		}
		return false;
	}

	public String getNumberTitle(String s) {
		compile = Pattern.compile(p33);
		matcher = compile.matcher(s);
		String s2 = "";
		if (matcher.find()) {
			s2 = s.replaceAll("[一二三四五六七八九十]、", "");
			s2 = s2.replaceAll("（.*）", "");
			return s2;
		}
		return s2;

	}

	public boolean isEmpty(String s) {

		compile = Pattern.compile("^[\\s&&[^\\n]]\\s*$");
		matcher = compile.matcher(s);

		if (matcher.find()) {
			return true;
		}
		return false;

	}

	@Test
	public void tes() throws Exception {
		InputStream is = new FileInputStream(docPath);
		HWPFDocument doc = new HWPFDocument(is);
		Range range = doc.getRange();
		int numParagraphs = range.numParagraphs();
		String text;

		for (int i = 0; i < numParagraphs; i++) {
			text = range.getParagraph(i).text();
//			
//			// this.readList1(range);
////			boolean empty = isEmpty(text);
////			if (empty) {
////				continue;
////			}
//
			System.out.println(text);
		}
//	if(text.equals("\t")) {
//		continue;
	}

	// StringUtils.getParagraphs(text);
	// String pattern="\\( [A-D] \\)";
//			Pattern compile = Pattern.compile(pattern);
//			Matcher matcher = compile.matcher(text);
//			if(matcher.find()) {
//				
//			}

	private void readList1(Range range) {
		int num = range.numParagraphs();
		Paragraph para;
		for (int i = 0; i < num; i++) {
			para = range.getParagraph(i);
			if (para.isInList()) {
				System.out.println(i + "list: " + para.text());
			}
		}
	}

	@Test
	public void testReadByExtractor() throws Exception {
		InputStream is = new FileInputStream("C:\\Users\\Administrator\\Desktop\\路由与交换练习题.doc");
		WordExtractor extractor = new WordExtractor(is);
		// 输出word文档所有的文本
		// System.out.println(extractor.getText());
		// System.out.println(extractor.getTextFromPieces());
		// //输出页眉的内容

		// 输出当前word文档的元数据信息，包括作者、文档的修改时间等。
		// System.out.println(extractor.getMetadataTextExtractor().getText());
		// 获取各个段落的文本
		String paraTexts[] = extractor.getParagraphText();

		for (int i = 0; i < paraTexts.length; i++) {
			System.out.println("Paragraph " + (i + 1) + " : " + paraTexts[i]);
		}
		// 输出当前word的一些信息
		// printInfo(extractor.getSummaryInformation());
		// 输出当前word的一些信息
		// printInfo(extractor.getDocSummaryInformation());
		this.closeStream(is);
	}

	private void printInfo(SummaryInformation info) {
		// 作者
		System.out.println(info.getAuthor());
		// 字符统计
		System.out.println(info.getCharCount());
		// 页数
		System.out.println(info.getPageCount());
		// 标题
		System.out.println(info.getTitle());
		// 主题
		System.out.println(info.getSubject());

	}

	private void printInfo(DocumentSummaryInformation info) {
		// 分类
		System.out.println(info.getCategory());
		// 公司
		System.out.println(info.getCompany());
	}

	/**
	 * 关闭输入流
	 * 
	 * @param is
	 */
	private void closeStream(InputStream is) {
		if (is != null) {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Test
	public void testReadByDoc() throws Exception {
		InputStream is = new FileInputStream("C:\\Users\\Administrator\\Desktop\\路由与交换练习题.doc");
		HWPFDocument doc = new HWPFDocument(is);
		// 输出书签信息
		this.printInfo(doc.getBookmarks());
		// 输出文本
		System.out.println(doc.getDocumentText());
		Range range = doc.getRange();
		// this.insertInfo(range);
		this.printInfo(range);
		// 读表格
		// this.readTable(range);
		// 读列表
		this.readList1(range);
		// 删除range
		Range r = new Range(2, 5, doc);
		// r.delete();//在内存中进行删除，如果需要保存到文件中需要再把它写回文件
		// 把当前HWPFDocument写到输出流中
		// doc.write(new FileOutputStream("D:\\test.doc"));
		this.closeStream(is);
	}

	/**
	 * 关闭输入流
	 * 
	 * @param is
	 */

	/**
	 * 输出书签信息
	 * 
	 * @param bookmarks
	 */
	private void printInfo(Bookmarks bookmarks) {
		int count = bookmarks.getBookmarksCount();
		System.out.println("书签数量：" + count);
		Bookmark bookmark;
		for (int i = 0; i < count; i++) {
			bookmark = bookmarks.getBookmark(i);
			System.out.println("书签" + (i + 1) + "的名称是：" + bookmark.getName());
			System.out.println("开始位置：" + bookmark.getStart());
			System.out.println("结束位置：" + bookmark.getEnd());
		}
	}

	/**
	 * 读表格 每一个回车符代表一个段落，所以对于表格而言，每一个单元格至少包含一个段落，每行结束都是一个段落。
	 * 
	 * @param range
	 */
//	   private void readTable(Range range) {  
//	      //遍历range范围内的table。  
//	      TableIterator tableIter = new TableIterator(range);  
//	      Table table;  
//	      TableRow row;  
//	      TableCell cell;  
//	      while (tableIter.hasNext()) {  
//	         table = tableIter.next();  
//	         int rowNum = table.numRows();  
//	         for (int j=0; j<rowNum; j++) {  
//	            row = table.getRow(j);  
//	            int cellNum = row.numCells();  
//	            for (int k=0; k<cellNum; k++) {  
//	                cell = row.getCell(k);  
//	                //输出单元格的文本  
//	                System.out.println(cell.text().trim());  
//	            }  
//	         }  
//	      }  
//	   }  

	/**
	 * 读列表
	 * 
	 * @param range
	 */
	private void readList(Range range) {
		int num = range.numParagraphs();
		Paragraph para;
		for (int i = 0; i < num; i++) {
			para = range.getParagraph(i);
			if (para.isInList()) {
				System.out.println("list: " + para.text());
			}
		}
	}

	/**
	 * 输出Range
	 * 
	 * @param range
	 */
	private void printInfo(Range range) {
		// 获取段落数
		int paraNum = range.numParagraphs();
		System.out.println(paraNum);
		for (int i = 0; i < paraNum; i++) {
			// this.insertInfo(range.getParagraph(i));
			System.out.println("段落" + (i + 1) + "：" + range.getParagraph(i).text());
			if (i == (paraNum - 1)) {
				this.insertInfo(range.getParagraph(i));
			}
		}
		int secNum = range.numSections();
		System.out.println(secNum);
		Section section;
		for (int i = 0; i < secNum; i++) {
			section = range.getSection(i);
			System.out.println(section.getMarginLeft());
			System.out.println(section.getMarginRight());
			System.out.println(section.getMarginTop());
			System.out.println(section.getMarginBottom());
			System.out.println(section.getPageHeight());
			System.out.println(section.text());
		}
	}

	/**
	 * 插入内容到Range，这里只会写到内存中
	 * 
	 * @param range
	 */
	private void insertInfo(Range range) {
		range.insertAfter("Hello");
	}

}
